package com.example.student;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.TextView;

import com.example.student.activity.AddTaskActivity;
import com.example.student.adapter.SubjectAdapter;
import com.example.student.bean.SubjectBean;
import com.example.student.fragment.DoTaskFragment;
import com.example.student.fragment.MyFragment;
import com.example.student.fragment.TaskRecordFragment;
import com.example.student.myinterface.PopInterface;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, PopInterface {

    private FrameLayout fragmentContainer;
    private RadioButton rb_doTask, rb_record, rb_my;

    private TaskRecordFragment taskRecordFragment;
    private DoTaskFragment doTaskFragment;
    private MyFragment myFragment;
    private String TAG = "MainActivity";

    protected PopupWindow popupWindow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mymain);
        initView();
        initLister();
        initRecordfragment();
    }

    private void initView() {
        fragmentContainer = findViewById(R.id.fragmentContainer);
        rb_record = findViewById(R.id.rb_record);
        rb_doTask = findViewById(R.id.rb_doTask);
        rb_my = findViewById(R.id.rb_my);

    }

    private void initLister() {
        rb_record.setOnClickListener(this);
        rb_doTask.setOnClickListener(this);
        rb_my.setOnClickListener(this);

    }

    private void initDoTaskfragment() {
        rb_record.setTextColor(getColor(R.color.r));
        rb_doTask.setTextColor(getColor(R.color.l));
        rb_my.setTextColor(getColor(R.color.r));
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        if (doTaskFragment == null) {
            doTaskFragment = new DoTaskFragment(MainActivity.this);
            transaction.add(R.id.fragmentContainer, doTaskFragment);
        }
        hideAllFragement();
        transaction.show(doTaskFragment);
        transaction.commit();
    }

    private void initMyTaskfragment() {
        rb_record.setTextColor(getColor(R.color.r));
        rb_doTask.setTextColor(getColor(R.color.r));
        rb_my.setTextColor(getColor(R.color.l));
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        if (myFragment == null) {
            myFragment = MyFragment.newInstance("","");
            transaction.add(R.id.fragmentContainer, myFragment);
        }
        hideAllFragement();
        transaction.show(myFragment);
        transaction.commit();
    }

    private void initRecordfragment() {
        rb_record.setTextColor(getColor(R.color.l));
        rb_doTask.setTextColor(getColor(R.color.r));
        rb_my.setTextColor(getColor(R.color.r));
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        if (taskRecordFragment == null) {
            taskRecordFragment = new TaskRecordFragment();
            transaction.add(R.id.fragmentContainer, taskRecordFragment);
        }
        hideAllFragement();
        transaction.show(taskRecordFragment);
        transaction.commit();
    }

    private void hideAllFragement() {
//        initTransaction();
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();

        if (taskRecordFragment != null) {
            transaction.hide(taskRecordFragment);
        }
        if (doTaskFragment != null) {
            transaction.hide(doTaskFragment);
        }
        if (myFragment != null) {
            transaction.hide(myFragment);
        }
        transaction.commit();
//        原文链接：https://blog.csdn.net/gz2012zb/article/details/81516700

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rb_record:

                initRecordfragment();
                Log.e(TAG,"//..记录");

                break;
            case R.id.rb_doTask:

                Log.e(TAG,"//..去做作业");
                initDoTaskfragment();
                break;
            case R.id.rb_my:

                Log.e(TAG,"//..我的作业");
                initMyTaskfragment();
                break;

            case R.id.tv_sure:
                popupWindow.dismiss();
                break;

        }

    }

    @Override
    public void setPopShow(String flag) {
        Log.e(TAG,"//..手动了吗"+flag);

        showPopWin(flag);



    }




    public void showPopWin(String flag) {

        View popview = LayoutInflater.from(this).inflate(R.layout.pop_change_state, null);

        EditText et_state = popview.findViewById(R.id.et_state);
        TextView tv_sure = popview.findViewById(R.id.tv_sure);

        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);

        int winWidth = (int) (metrics.widthPixels * 0.75);
        backgroundAlpha(0.5f);

        popupWindow = new PopupWindow(popview, winWidth, ViewGroup.LayoutParams.WRAP_CONTENT);

        ColorDrawable dw = new ColorDrawable(0x00);
        popupWindow.setBackgroundDrawable(dw);

        popupWindow.setOutsideTouchable(true);
        popupWindow.setFocusable(true);

        popupWindow.showAtLocation(fragmentContainer, Gravity.CENTER, 0, 20);
        popupWindow.setOnDismissListener(new PopupDismissListener());

        tv_sure.setOnClickListener(MainActivity.this);

    }


    class PopupDismissListener implements PopupWindow.OnDismissListener {
        @Override
        public void onDismiss() {
            backgroundAlpha(1f);
        }
    }

    /**
     * 设置屏幕的透明度
     */
    protected void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        getWindow().setAttributes(lp);
    }
}